package com.job.admin.job.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.job.admin.common.PageModel;
import com.job.admin.framework.exception.JobException;
import com.job.admin.job.dao.JobDao;
import com.job.admin.job.model.JobInfoModel;
import com.job.admin.job.schedule.JobDynamicScheduler;
import com.job.admin.job.service.JobService;
@Service
public class JobServiceImpl implements JobService {
    @Resource
    private JobDao jobDao;
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor=Exception.class)
    @Override
    public void saveJobInfoModel(JobInfoModel jobInfoModel) throws Exception {
	try {
	    validateJobData(jobInfoModel);
	    jobDao.saveJobInfoModel(jobInfoModel);
	} catch (Exception e) {
	    e.printStackTrace();
	    throw new Exception("保存【"+jobInfoModel.getExecutorHandler()+"】异常");
	}
	
    }

    @Override
    public void updateJobIndoModel(JobInfoModel jobInfoModel) throws Exception {
	try {
	    validateJobData(jobInfoModel);
	    jobDao.updateJobIndoModel(jobInfoModel);
	} catch (Exception e) {
	    e.printStackTrace();
	    throw new Exception("修改【"+jobInfoModel.getExecutorHandler()+"】异常");
	}
    }
    /**
     * 
     * @Title: validateJobData
     * @Description:验证job信息
     * @author:sunwei
     * @createTime:2017年6月19日下午1:16:14
     * @param infoModel
     * @throws Exception 
     */
    private void validateJobData(JobInfoModel infoModel) throws Exception{
	if (!CronExpression.isValidExpression(infoModel.getJobCron())) {
		throw new Exception("请输入格式正确的“Cron”");
	}
	// childJobKey valid
	if (StringUtils.isNotBlank(infoModel.getChildJobKey())) {
	    String[] childJobKeys = infoModel.getChildJobKey().split(",");
	    for (String childJobKeyItem : childJobKeys) {
		String[] childJobKeyArr = childJobKeyItem.split("_");
		if (childJobKeyArr.length != 2) {
		    throw new Exception("【"+childJobKeyItem+"】子任务Key({0})格式错误");
		}
		JobInfoModel childJobInfo = jobDao.queryJobById(Long.valueOf(childJobKeyArr[1]));
		if (childJobInfo == null) {
		   throw new Exception ("【"+childJobKeyItem+"】子任务Key({0})无效");
		}
	    }
	}
    }
    
    /**
     * 
     * @Title: validateJobInfo
     * @Description:
     * @author:sunwei
     * @createTime:2017年6月14日上午9:56:01
     * @param jobInfoModel
     * @throws Exception
     */
    private void validateJobInfo(JobInfoModel jobInfoModel) throws Exception{
	if(!CronExpression.isValidExpression(jobInfoModel.getJobCron())){//
	    throw new Exception("请输入格式正确的“Cron");
	}
    }
    
    /**
     * 
     * @Title: queryPageList
     * @Description:查询分页集合
     * @author:sunwei
     * @createTime:2017年6月12日下午5:13:51
     * @param pageModel
     * @param jobInfoModel
     * @return
     */
    @Override
    public PageModel queryPageList(PageModel pageModel,JobInfoModel jobInfoModel){
	pageModel.setCountSql("JobInfoMapper.pageListCount");
	return jobDao.queryPageList(pageModel, jobInfoModel);
    }

    @Override
    public JobInfoModel queryJobById(Long id) {
	return jobDao.queryJobById(id);
    }

    @Override
    public void deleteById(Long id) throws Exception {
	jobDao.deleteById(id);
    }

    @Override
    public void updateJobState(JobInfoModel jobInfoModel) {
	jobDao.updateJobState(jobInfoModel);
	
    }
    @Override
    public List<JobInfoModel> queryJobInfoModels(JobInfoModel jobInfoModel){
	return jobDao.queryJobs(jobInfoModel);
    }
    
    @Override
    public void executorJob(Long id) throws JobException {
	JobInfoModel jobInfoModel = jobDao.queryJobById(id);
        String group = String.valueOf(jobInfoModel.getJobGroup());
        String name = String.valueOf(jobInfoModel.getId());
        
	try {
		JobDynamicScheduler.triggerJob(name, group);
	} catch (SchedulerException e) {
		e.printStackTrace();
		throw new JobException("执行任务异常："+e.getMessage());
	}
        
    }
    
}
